﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SmartSpace.Silverlight
{
    public class HThicknessWrapper : FrameworkElement
	{
		public FrameworkElement Target
		{
			get
			{
				return (FrameworkElement)GetValue(TargetProperty);
			}
			set
			{
				SetValue(TargetProperty, value);
			}
		}

		// Using a DependencyProperty as the backing store for Target.  This enables animationIn, styling, binding, etc...
		public static readonly DependencyProperty TargetProperty =
            DependencyProperty.Register("Target", typeof(FrameworkElement), typeof(HThicknessWrapper), new PropertyMetadata(null, OnTargetChanged));


		static void OnTargetChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
		{
            HThicknessWrapper sender = (HThicknessWrapper)source;
			sender.UpdateMargin();
		}

		public String PropertyName
		{
			get
			{
				return (String)GetValue(PropertyNameProperty);
			}
			set
			{
				SetValue(PropertyNameProperty, value);
			}
		}

		// Using a DependencyProperty as the backing store for PropertyName.  This enables animationIn, styling, binding, etc...
		public static readonly DependencyProperty PropertyNameProperty =
            DependencyProperty.Register("PropertyName", typeof(String), typeof(HThicknessWrapper), new PropertyMetadata("Margin"));

		public double Value
		{
			get
			{
				return (double)GetValue(ValueProperty);
			}
			set
			{
				SetValue(ValueProperty, value);
			}
		}

		// Using a DependencyProperty as the backing store for Value.  This enables animationIn, styling, binding, etc...
		public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(HThicknessWrapper), new PropertyMetadata(0.0, OnValueChanged));


		static void OnValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
		{
            HThicknessWrapper sender = (HThicknessWrapper)source;
			sender.UpdateMargin();
		}





		static void OnPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
		{
            HThicknessWrapper sender = (HThicknessWrapper)source;
			sender.UpdateMargin();
		}

		private void UpdateMargin()
		{
            if (Target != null)
            {
                var thicknessProperty = Target.GetType().GetProperty(PropertyName);
                var currentThickness = (Thickness)thicknessProperty.GetValue(Target, null);
                var nextThickness = new Thickness(
                    Value,
                    currentThickness.Top,
                    -1 * Value,
                    currentThickness.Bottom);

                thicknessProperty.SetValue(Target, nextThickness, null);
            }
		}
	}
}
